// Copyright (c) 2007 Alistair Jones
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.hypirinha.html.codegen.MemberExpectations.*;
import static org.hypirinha.html.codegen.MemberExpectations.member;

import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;

public class AttributeFactoryTest {

    @Test
    public void shouldGenerateMethodsThatInstantiateAttributes() {
        List<String> elements = new ArrayList<String>();
        elements.add("muddy");
        elements.add("breezy");
        elements.add("hot");
        elements.add("wet");
        AttributeFactory factory = new AttributeFactory(elements);
        GeneratedClass generatedClass = factory.createImplementation();

        List<Member> members = generatedClass.getMembers();
        assertEquals(4, members.size());

        ConcreteMethod method = (ConcreteMethod) members.get(0);
        assertEquals("breezy", method.getMethodName());
        assertEquals("BreezyAttribute", method.getReturnType());
        assertEquals("return new BreezyAttribute(value);", method.getMethodBody());
        assertTrue(method.getModifiers().contains("static"));
    }

    @Test
    public void shouldModifyMethodNamesForAttributesThatClashWithJavaKeywords() {
        List<String> elements = new ArrayList<String>();
        elements.add("char");
        elements.add("class");
        elements.add("for");
        AttributeFactory factory = new AttributeFactory(elements);
        GeneratedClass generatedClass = factory.createImplementation();

        List<Member> members = generatedClass.getMembers();
        assertEquals(3, members.size());

        ConcreteMethod method0 = (ConcreteMethod) members.get(0);
        assertEquals("charr", method0.getMethodName());
        assertEquals("CharAttribute", method0.getReturnType());
        assertEquals("return new CharAttribute(value);", method0.getMethodBody());

        ConcreteMethod method1 = (ConcreteMethod) members.get(1);
        assertEquals("classs", method1.getMethodName());
        assertEquals("ClassAttribute", method1.getReturnType());
        assertEquals("return new ClassAttribute(value);", method1.getMethodBody());

        ConcreteMethod method2 = (ConcreteMethod) members.get(2);
        assertEquals("forr", method2.getMethodName());
        assertEquals("ForAttribute", method2.getReturnType());
        assertEquals("return new ForAttribute(value);", method2.getMethodBody());
    }


}